package com.dp.service.impl;

import java.io.File;
import java.text.DecimalFormat;
import java.util.List;

import javax.persistence.EntityNotFoundException;
import javax.persistence.Query;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dp.service.impl3.BaseImpl;
import com.dp.util.DataMap;
import com.dp.vo.NewsPlatforminfo;
import com.dp.vo.NewsUserupload;

@Service
@Transactional
public class UseruploadServiceImpl extends BaseImpl {
	
	/**
	 * Find all the NewsUserupload Entity
	 * @param hashMap
	 * @param firstrow
	 * @param maxrow
	 * @return List<NewsUserupload>
	 */
	@Transactional(readOnly = true)
	public List<NewsUserupload> findAllNewsUseruploadEntity(DataMap hashMap, Integer firstrow,
			Integer maxrow){
		NewsUserupload userupload=(NewsUserupload) hashMap.get("userupload");
		List<NewsUserupload> queryList=null;
		try {
			StringBuffer sf = new StringBuffer("SELECT n FROM NewsUserupload n WHERE 1=1 ");
			if(userupload!=null){
				if(userupload.getClientid()!=null&& userupload.getClientid().toString().trim().length() > 0){
					sf.append(" AND n.clientid='"
							+ userupload.getClientid().toString().trim() + "'");
				}
				if(userupload.getUsersetname()!=null&& userupload.getUsersetname().toString().trim().length() > 0){
					sf.append(" AND n.usersetname like '%"
							+ userupload.getUsersetname().toString().trim() + "%'");
				}
				if(userupload.getIsok()!=null&& userupload.getIsok().toString().trim().length() > 0){
					sf.append(" AND n.isok like '%"
							+ userupload.getIsok().toString().trim() + "%'");
				}
				if(userupload.getIscheck()!=null&& userupload.getIscheck().toString().trim().length() > 0){
					sf.append(" AND n.ischeck like '%"
							+ userupload.getIscheck().toString().trim() + "%'");
				}
			}
			sf.append(" AND isdeletebyuser!=2 AND isdelete!=2");
			sf.append(" order by fileid desc,ischeck");
			Query query = em.createQuery(sf.toString());
			if (firstrow != null) {
				query.setFirstResult(firstrow);
			}
			if (maxrow != null) {
				query.setMaxResults(maxrow);
			}
			queryList = query.getResultList();
			DecimalFormat decimalformat = new DecimalFormat("0.00");
			for(NewsUserupload upload:queryList){
				if(upload.getFilelength()!=null){
					upload.setFilelength(decimalformat.format(Double.parseDouble(upload.getFilelength())/1024));
				}
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return queryList;
	}

	/**
	 * findNewsUseruploadById
	 * @param fileid
	 * @return	NewsUserupload
	 */
	public NewsUserupload findNewsUseruploadById(Integer fileid) {
		NewsUserupload o = null;
		try {
			o = this.em.find(NewsUserupload.class, fileid);
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		if (o == null) {
			String msg = "Uh oh, '" + NewsPlatforminfo.class + "' object with id '" + fileid
					+ "' not found...";
			logger.warn(msg);
			throw new EntityNotFoundException(msg);
		}
	
		return o;
	}

	/**
	 * Ischecked
	 * Update set ischeck=1     	
	 * @param NewsPlatforminfo
	 * @return Boolean
	 */
	public Boolean Ischecked(NewsUserupload userupload) {
		Boolean resultb=true;
		try {
			String	jpql="UPDATE NewsUserupload n SET n.ischeck=:ischeck WHERE n.fileid=:fileid";
			Query	query = em.createQuery(jpql);
			query.setParameter("ischeck", userupload.getIscheck());
			query.setParameter("fileid", userupload.getFileid());
			int iCode=query.executeUpdate();
			if(iCode<0){
					resultb=false;
			}
		} catch (Exception e) {
			logger.warn(e.getMessage());
			e.printStackTrace();
			resultb=false;
		}
		return resultb;
	}

	/**
	 * deleteNewsUseruploadById 
	 * Update set isdelete=2
	 * @param urlB
	 * @return boolean b
	 */
	public boolean deleteNewsUserupload(NewsUserupload userupload) {
		boolean b = false;
		try {
			String filepath = new String(userupload.getFullpath());   
			Runtime rt = Runtime.getRuntime();   
			File file = new File(filepath);   
			if(file.exists()){
				boolean flag  = file.delete();   
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		try {
			String jpql="UPDATE NewsUserupload n SET n.isdelete=:isdelete WHERE n.fileid=:fileid";
			Query query = em.createQuery(jpql);
			query.setParameter("isdelete", userupload.getIsdelete());
			query.setParameter("fileid", userupload.getFileid());
			int iCode=query.executeUpdate();
			if(iCode<0){
				b = false;
			}else{
				b = true;
			}
		} catch (Exception e) {
			logger.warn("deleteNewsPlatforminfoById fail !!!" + e.getMessage());
			e.printStackTrace();
		} 
		return b;
	}

}
